package com.obst.tracenote;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.androidquery.AQuery;
import com.androidquery.auth.FacebookHandle;
import com.androidquery.callback.AbstractAjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;
import com.obst.tracenote.adapter.LoginAdapter;
import com.obst.tracenote.data.ListData;
import com.obst.tracenote.listcontroller.ListController;
import com.obst.tracenote.util.FacebookAPI;
import com.obst.tracenote.util.MyPreferences;
import com.obst.tracenote.util.MyServer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class LoginAct extends Activity {
	private ArrayList<ListData> mList;
	private Facebook mFacebook = new Facebook(C.FACEBOOK_APP_ID);
	private String mFacebookAccessToken;
	private static String FB_PERMISSIONS = "read_stream,read_friendlists,manage_friendlists,manage_notifications,publish_stream,publish_checkins,offline_access,user_photos,user_likes,user_groups,friends_photos";
	private MyServer mServer;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_body);

		mList = new ArrayList<ListData>();
		//{"data" : [{"tid":"30","uid":"1","tracetype":"0","hidecnt":"0","title":"%EC%9D%B4%EA%B1%B4+%EB%91%90%EB%B2%88%EC%A7%B8+%ED%9D%94%EC%A0%81%EC%9D%B4%EA%B3%A0","rdate":"2012-01-15 00:00:00+09","path":["1_30_1326615605.jpg"]}]}
		String tmpJson = "{\"data\":[{\"title\":\"facebook\"},{\"title\":\"twitter\"},{\"title\":\"google\"}]}";
		JSONObject jo = null;
		try {
			jo = new JSONObject(tmpJson);
			JSONArray tmpJa = jo.getJSONArray("data");
			int tmpLen	= tmpJa.length();
			JSONObject jo2 = new JSONObject();
			for (int i = 0; i < tmpLen; i++) {
				jo2 = tmpJa.getJSONObject(i);
				mList.add(new ListData(jo2));
			}			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		ListView myListView = (ListView) findViewById(R.id.list);
		FacebookAPI fbApi = new FacebookAPI(mFacebook, LoginAct.this);
		LoginAdapter tmpAdapter	= new LoginAdapter(LoginAct.this, R.layout.login_adapter, mList, fbApi);
		ListController loginList	= new ListController(LoginAct.this, myListView, tmpAdapter);
		loginList.addView();
		loginList.showView();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.v(C.LOG_TAG, "facebook RESULT : " + Integer.toString(requestCode));
		
		//mFacebook.authorizeCallback(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == C.FACEBOOK_AUTH_CODE) {
				mFacebook.authorizeCallback(requestCode, resultCode, data);
			}
		} else {
			if (requestCode == C.FACEBOOK_AUTH_CODE) {
				mFacebook.authorizeCallback(requestCode, resultCode, data);
			}
		}
	}
}
